package net.kem.android.positioner.responsehandler;

import java.util.ArrayList;

import net.kem.android.positioner.MessageObject;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 *
 <message>
	<phoneNumber>0507362270</phoneNumber>	<!-- Sender's phone number. For debug purposes only -->
	<status>0</status>         <!-- Integer. 0 if get Objects operation was successful. -->
	<descr>bla.bla</descr>     <!-- Optional string. Contains error description if status != 0 -->
	<objects>
		<object>
			<idObject>1</idObject>
			<idCategory>1</idCategory>
			<idObjectType>3</idObjectType>
			<name>Living Room</name>
			<state>StatusAsString</state>
		</object>
		<object>
			<idObject>2</idObject>
			<idObjectType>3</idObjectType>
			<name>Hall</name>
			<state>StatusAsString</state>
		</object>
	</objects>
  </message>
 * @author Zhenya
 *
 */
public class GetAllObjectsResponseHandler extends ResponseHandler {
//	class POI {
//		private int id;
//		private int type;
//		private String name;
//		private float lat;
//		private float lon;
//		private int accuracy; // Reserved for future protocol extensions.
//		private int radius; // Reserved for future protocol extensions.
//		private String notes; // Reserved for future protocol extensions.
//	}

	private ArrayList<MessageObject> messageObjects;
	private MessageObject currentMessageObject;

	@Override
	public void startDocument() throws SAXException {
		messageObjects = new ArrayList<MessageObject>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		super.startElement(uri, localName, qName, atts);
		if(localName.equals("object")) {
			currentMessageObject = new MessageObject();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(localName.equals("object")) {
			messageObjects.add(currentMessageObject);
		} else if(localName.equals("idObject")) {
			currentMessageObject.setId(Integer.parseInt(currentValue));
		} else if(localName.equals("idCategory")) {
			currentMessageObject.setCategory(Integer.parseInt(currentValue));
		} else if(localName.equals("idObjectType")) {
			currentMessageObject.setType(Integer.parseInt(currentValue));
		} else if(localName.equals("name")) {
			currentMessageObject.setName(currentValue);
		} else if(localName.equals("state")) {
			currentMessageObject.setState(currentValue);
		}
		else {
			super.endElement(uri, localName, qName);
		}
	}

	public ArrayList<MessageObject> getMessageObjects() {
		return messageObjects;
	}
}